package com.feng.modules.plat.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.util.WebUtils;

import com.feng.modules.core.service.impl.BaseService;
import com.feng.modules.core.util.BaseUtils;
import com.feng.modules.plat.dao.ILoginDao;
import com.feng.modules.plat.service.ILoginService;

@SuppressWarnings({ "rawtypes", "unchecked" })
public class LoginServiceImpl extends BaseService implements ILoginService {
	private ILoginDao loginDao;


	public void setLoginDao(ILoginDao loginDao) {
		this.loginDao = loginDao;
	}

	public int saveLogin(Map<String, Object> map, HttpServletRequest request) {
		String password = (String) map.get("PASSWORD");
		List userNameList = loginDao.getUserName(map);
		map.put("PASSWD", BaseUtils.getChangePassword(password));
		List valiList = loginDao.validatePassword(map);
		if (userNameList.isEmpty()) {
			return 0;
		}
		if (valiList.isEmpty()) {
			return 1;
		} else {
			Map sessionMap = new HashMap();
			sessionMap = (Map) valiList.get(0);
			sessionMap.put("IP", request.getRemoteAddr());
			sessionMap.remove("PASSWORD");
			WebUtils.setSessionAttribute(request, "user", sessionMap);
			sessionMap.put("LOG_ID", BaseUtils.newUUID());
			if (StringUtils.isBlank((String) map.get("CATEGORY"))) {
				sessionMap.put("CATEGORY", "security");
			}
			loginDao.saveLog(sessionMap);
			return 2;
		}
	}

	@Override
	public void logout(Map parametersToMap, HttpServletRequest servletRequest) {
		// TODO Auto-generated method stub
		
	}
}
